// pages/select_time/index.js
const app     = getApp()
const request = require('../../utils/request.js');
const config = require('../../utils/config.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
       
        selectIndex:[0,-1],
        times:[],
        select_time:[],
        type:0,
        time_select:'',
        hosptial_id:0,
        appoints_id:0,
        appoints:null,
        is_submit:false,
    },
    chooseDate(e){
       var that =this;
       var  index = e.currentTarget.dataset.index;
     
       var selectIndex=that.data.selectIndex;

       if(that.data.selectIndex[0]!=index){
        that.setData({
            times:that.data.select_time[index].times_arr
        })  
       }else{
            that.setData({
                time_select:''
            })
       }
       selectIndex[0]=index;
       selectIndex[1]=-1;
       console.log(index)
       that.setData({
           selectIndex:selectIndex,
       })
       
         
    },
    choseTime(e){
        var that =this;
       var  index = e.currentTarget.dataset.index;
     console.log(index)
       var selectIndex=that.data.selectIndex;
       selectIndex[1]=index;
       console.log(that.data.times[index].time_solt);
       that.setData({
        selectIndex:selectIndex,
        times_slot:that.data.times[index].time_solt,
    })
    },
    sureDate(){
        var that =this;
       if(that.data.is_submit){
           wx.showToast({
             title: '请勿重复提交',
             icon:'none'
           })
           return false
       }
       if(that.data.selectIndex[1]==-1){
           request.toast('请选择预约时间');
           return;
       }
       that.setData({
        is_submit:true
       })
       var choice_date={ 'date':that.data.select_time[that.data.selectIndex[0]].date,
          'week_name':that.data.select_time[that.data.selectIndex[0]].week_name,
           'date_min':that.data.select_time[that.data.selectIndex[0]].date_min,
           'text':that.data.select_time[that.data.selectIndex[0]].date_min+'('+that.data.select_time[that.data.selectIndex[0]].week_name+')'+that.data.times_slot,
           'time_solt':that.data.times_slot};
       let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。
       let prevPage = pages[ pages.length - 2 ];
      
       if(prevPage.route=='pages/appointment_detail/index'){
           
       
          var json = {};
          json.id=that.data.appoints_id
          json.appoint_times=choice_date.time_solt;
          json.appoint_week_name=choice_date.week_name;
          json.appoint_date=choice_date.date;
          json.appoint_text=json.appoint_date+'('+json.appoint_week_name+')'+json.appoint_times;
   
           request.requestLoading(
               "/api/appoint/updateAppointment",
               json,
               'GET',
             ).then((res) => {
              
                
              if(res.code==1){
                wx.showToast({
                    title: '改约成功',
                    icon:'none',
                    duration:1500
                  })
                  prevPage.getDetail()
                  setTimeout(function(){
                    wx.navigateBack({
                        delta:1
                      })
                  },1500)
              }else{
                wx.showToast({
                    title: res.msg,
                    icon:'none'
                  })
                   that.setData({
                    is_submit:false
                   })
              }
          
              
         
             }).catch((err) => {
               console.error(err);
             });
       }else if(prevPage.route=='pages/my_appointment/index'){
        var json = {};
       json.id=that.data.appoints_id
        json.appoint_times=choice_date.time_solt;
        json.appoint_week_name=choice_date.week_name;
        json.appoint_date=choice_date.date;
        json.appoint_text=json.appoint_date+'('+json.appoint_week_name+')'+json.appoint_times;
 
         request.requestLoading(
             "/api/appoint/updateAppointment",
             json,
             'GET',
           ).then((res) => {
            
              
            if(res.code==1){
            
                wx.showToast({
                    title: '改约成功',
                    icon:'none',
                    duration:1500
                  })
                  prevPage.init_Data()
                  setTimeout(function(){
                    wx.navigateBack({
                        delta:1
                      })
                  },1500)
            }else{
                wx.showToast({
                    title: res.msg,
                    icon:'none'
                  })
                  that.setData({
                    is_submit:false
                   })
            }
        
            
       
           }).catch((err) => {
             console.error(err);
           });
       }else if(prevPage.route=='pages/make_appointment/index'){
        prevPage.setData({ // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
            choice_date:choice_date,
           })
           wx.navigateBack({
            delta:1
          })
       }
     
       //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。
      
    
     
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options)
     this.setData({
        hosptial_id:options.hosptial_id
     })
     if(options.appoints_id){
        this.setData({
            appoints_id:options.appoints_id
        })
        this.getAppint()
     }else{
        this.getDetail()
     }
     if(options.type){
         this.setData({
             type:1
         })
     }
    
    },
    getAppint(){
        var that = this;
        var json = {};
       
       json.id=that.data.appoints_id;

        request.requestLoading(
            "/api/appoint/appointDetail",
            json,
            'GET',
          ).then((res) => {
           
        that.setData({
            appoints:res.data
        })
        that.getDetail()
       
           
      
          }).catch((err) => {
            console.error(err);
          });
    },
    getDetail()
    {
        var that = this;
        var json = {};
       
       json.hospital_id=that.data.hosptial_id;

        request.requestLoading(
            "/api/appoint/getAppoints",
            json,
            'GET',
          ).then((res) => {
           
        that.setData({
            select_time:res.data,
        })
        if(that.data.appoints){
            var selectIndex=that.data.selectIndex;
             var is_select=false;
            for (var i = 0; i <that.data.select_time.length; i++) {
                if(that.data.appoints.appoint_date==that.data.select_time[i].date){
                    selectIndex[0]=i;
                    console.log(i+'w1111')
                    console.log(that.data.select_time[i])
                    that.setData({
                        times:that.data.select_time[i].times_arr
                    })
                     for (var j = 0; j <that.data.times.length; j++) {
                         if(that.data.appoints.appoint_times==that.data.times[j].time_solt){
                             selectIndex[1]=j;
                             that.setData({
                             times_slot:that.data.times[j].time_solt
                             })
                         }
                     }
                     that.setData({
                        selectIndex:selectIndex,
                        
                    })
                }
              }
              if(!is_select){
                that.setData({
                    times:that.data.select_time[0].times_arr
                })  
              }
           
        }else{
            that.setData({
                times:that.data.select_time[0].times_arr
            })  
        }
       
        console.log(that.data.times)
       
           
      
          }).catch((err) => {
            console.error(err);
          });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        if(this.data.appoints_id){
            this.getAppint()
         }else{
            this.getDetail()
         }
   
        setTimeout(function(){
          wx.stopPullDownRefresh();
        },500)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

   
})